//
//  RightSearchViewController.m
//  PerfectWorld
//
//  Created by dllo on 16/8/29.
//  Copyright © 2016年 LST. All rights reserved.
//  Nav 右按钮 跳转的VC

#import "RightSearchViewController.h"
#import "rightSearchTableViewCell.h"
#import "AFNetworking.h"
#import "ListModel.h"
#import "SearchModel.h"
#import "AreaList.h"
#import "CityViewController.h"
#import "RightSearchSonViewController.h"
@interface RightSearchViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong) UISearchBar *BYsearchTextFd;
@property (nonatomic, strong)SearchModel *searchModel;
@end

@implementation RightSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 右按钮
    UIButton *rightNavBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightNavBtn.frame = CGRectMake(0, 0, 50, 30);
    [rightNavBtn setTitle:@"取消" forState:UIControlStateNormal];
    [rightNavBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightNavBtn];
    [rightNavBtn addTarget:self action:@selector(clickRightCancel:) forControlEvents:UIControlEventTouchUpInside];
    // 左
    self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftBtn.frame = CGRectMake(5, 5, 50, 30);
    [_leftBtn setTitle:@"广州" forState:UIControlStateNormal];
    [_leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_leftBtn];
    [_leftBtn addTarget:self action:@selector(clickLeftBtn:) forControlEvents:UIControlEventTouchUpInside];
    // 搜索
    UISearchBar *bar = [[UISearchBar alloc] init];
    self.navigationItem.titleView = bar;
    self.BYsearchTextFd = [[UISearchBar alloc]initWithFrame:CGRectMake(50, 33,self.view.frame.size.width - 150,31)];
    self.BYsearchTextFd.placeholder = @"搜索你感兴趣的小区";
    self.BYsearchTextFd.keyboardType = UIKeyboardTypeWebSearch;
    UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"搜索"]];
    img.frame = CGRectMake(5, 12 ,20,20);
    [self.BYsearchTextFd addSubview:img];
        self.view.backgroundColor = [UIColor colorWithRed:0.97 green:0.39 blue:0.53 alpha:1.00];
    self.BYsearchTextFd.barStyle = UIBarStyleDefault;
    self.navigationItem.titleView = self.BYsearchTextFd;
    self.urlStr = @"http://api.guju.com.cn/v2/building/recommend?cityId=441&start=1";
    [self handData];
    [self creatTableView];
    
    
}
- (void)handData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:_urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:responseObject];
        self.searchModel = [SearchModel modelWithDic:dic];
        [_tableView reloadData];
        
           } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败");
    }];
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 35, self.view.bounds.size.width, self.view.bounds.size.height)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"rightSearchTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(5, 0, self.view.bounds.size.width, 35)];
    header.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:header];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, header.frame.size.width, header.frame.size.height)];
    label.text = @"热门楼盘";
    label.font = [UIFont systemFontOfSize:15];
    [header addSubview:label];
    
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.bounds.size.height / 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _searchModel.list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    rightSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.listModel = _searchModel.list[indexPath.row];
    return cell;
}
- (void)clickRightCancel:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)clickLeftBtn:(UIBarButtonItem *)sender {
    CityViewController *cityVC = [[CityViewController alloc] init];
    cityVC.hidesBottomBarWhenPushed = YES;
    cityVC.PassAreaList = ^(AreaList *areaList) {
        [_leftBtn setTitle:areaList.areaName forState:UIControlStateNormal];
        _urlStr = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/building/recommend?cityId=%@&start=1", areaList.areaId];
        [self handData];
    };
    [self presentViewController:cityVC animated:YES completion:^{
    }];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    RightSearchSonViewController *rightSonVC = [[RightSearchSonViewController alloc] init];
    rightSonVC.urlStr = _searchModel.list[indexPath.row].id.description;
    [self.navigationController pushViewController:rightSonVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
